package com.qxsx.midi.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.HorizontalScrollView;
import android.widget.TextView;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.me.AmendAddressActivity;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.model.Order;
import com.qxsx.midi.model.ReceivingAddress;

import java.util.ArrayList;
import java.util.List;

public class MyAdressListAdapter extends BaseAdapter implements View.OnClickListener {

    Dialog dialog;
    private LayoutInflater inflater;

    private Context context;
    private int mScreentWidth;
    private List<ReceivingAddress> lists = new ArrayList<>();

    public MyAdressListAdapter(Context context, List<ReceivingAddress> lists, int screenWidth) {
        this.context = context;
        this.lists = lists;
        mScreentWidth = screenWidth;
        inflater = LayoutInflater.from(context);
    }

    public void clear () {
        this.lists.clear();
        notifyDataSetChanged ();
    }
    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public Object getItem(int position) {
        return lists.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public void AddData(List<ReceivingAddress> lists) {
        for (ReceivingAddress it : lists) {
            this.lists.add(it);
        }
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final  ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_list_my_adress, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.hSView = (HorizontalScrollView) convertView.findViewById(R.id.hsv);
//            viewHolder.name = (TextView) convertView.findViewById(R.id.txt_order_item_name);
//            viewHolder.desc = (TextView) convertView.findViewById(R.id.txt_order_desc);
//            viewHolder.price = (TextView) convertView.findViewById(R.id.txt_order_price);
//            viewHolder.btRmMsg = (TextView) convertView.findViewById(R.id.btn_rm_msg);
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tv_address = (TextView) convertView.findViewById(R.id.tv_address);
            viewHolder.tv_phone = (TextView) convertView.findViewById(R.id.tv_phone);
            viewHolder.tv_delete = (TextView) convertView.findViewById(R.id.btn_adr_delete);
            viewHolder.tv_moren = (TextView) convertView.findViewById(R.id.btn_adr_moren);
           // viewHolder.action = viewHolder.btRmMsg;    // (TextView) convertView.findViewById(R.id.btn_rm_msg);
            viewHolder.action = viewHolder.tv_delete;    // (TextView) convertView.findViewById(R.id.btn_rm_msg);
            viewHolder.action = viewHolder.tv_moren;    // (TextView) convertView.findViewById(R.id.btn_rm_msg);

//            viewHolder.content = ()
            //            viewHolder.tvContent = (TextView) convertView.findViewById(R.id.tv);
            //设置内容view的大小为屏幕宽度,这样按钮就正好被挤出屏幕外
            viewHolder.llContent =convertView.findViewById(R.id.msg_ll_context);
            ViewGroup.LayoutParams lp = viewHolder.llContent.getLayoutParams();
            lp.width = mScreentWidth;

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

//        viewHolder.name.setText(lists.get(position).name);
//        viewHolder.desc.setText(lists.get(position).desc);
//        viewHolder.price.setText(lists.get(position).price);
        final ReceivingAddress receivingAddress = lists.get(position);
        viewHolder.tv_name.setText(receivingAddress.getName());
        viewHolder.tv_delete.setTag(position);        // 保存位置
        viewHolder.tv_moren.setTag(position);        // 保存位置
        viewHolder.llContent.setTag(position);      // 保存位置

        final View finalConvertView = convertView;
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("adfasd", "asdfasdfasdf");
                Intent intent = new Intent(context, AmendAddressActivity.class);
//                Bundle bundle = new Bundle();
//                bundle.putString("id", "1");
//                intent.putExtras(bundle);
                context.startActivity(intent);

            }
        });


        //设置监听事件
        convertView.setOnTouchListener(new View.OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                switch (event.getAction())
                {
                    case MotionEvent.ACTION_UP:
                        //获得ViewHolder
                        ViewHolder viewHolder = (ViewHolder) v.getTag();
                        //获得HorizontalScrollView滑动的水平方向值.
                        int scrollX = viewHolder.hSView.getScrollX();
                        //获得操作区域的长度
                      //  int actionW = viewHolder.action.getWidth();
                        int actionW = 181;
                        //注意使用smoothScrollTo,这样效果看起来比较圆滑,不生硬
                        //如果水平方向的移动值<操作区域的长度的一半,就复原
                        if (scrollX < actionW / 2)  {
                            viewHolder.hSView.smoothScrollTo(0, 0);
                            viewHolder.llContent.setBackgroundColor(finalConvertView.getResources().getColor(R.color.theme_white));

                        }
                        else  { //否则的话显示操作区域
                            viewHolder.hSView.smoothScrollTo(actionW, 0);
                            viewHolder.llContent.setBackgroundColor(finalConvertView.getResources().getColor(R.color.theme_green));

                        }
                        return true;
                }
                return false;
            }
        });

        //这里防止删除一条item后,ListView处于操作状态,直接还原
        if (viewHolder.hSView.getScrollX() != 0)
        {
            viewHolder.hSView.scrollTo(0, 0);
            viewHolder.llContent.setBackgroundColor(finalConvertView.getResources().getColor(R.color.theme_white));
        }

        //设置背景颜色,设置填充内容.
//        viewHolder.content.setBackgroundResource(colors.get(position));
//        viewHolder.tvContent.setText("" + position);

        //设置监听事件
        viewHolder.tv_delete.setOnClickListener(this);
        viewHolder.tv_moren.setOnClickListener(this);
        viewHolder.llContent.setOnClickListener(this);
        return convertView;
    }

    class ViewHolder {
        public HorizontalScrollView hSView;
//        public TextView name;
//        public TextView desc;
//        public TextView price;
//        public TextView btRmMsg;
        public View action;
        public View llContent;
        TextView tv_moren;
        TextView tv_delete;
        TextView tv_name;
        TextView tv_phone;
        TextView tv_address;
    }

    @Override
    public void onClick(View v)
    {
        int position = (Integer) v.getTag();
        switch (v.getId())
        {
            case R.id.btn_adr_delete:
                lists.remove(position);
                break;
            case R.id.btn_adr_moren:
                //默认
                break;
            case R.id.msg_ll_context:
                Intent intent = new Intent(context, AmendAddressActivity.class);
                context.startActivity(intent);
//                LayoutInflater layout = LayoutInflater.from(AppContext.getContext());
//                View view = layout.inflate(R.layout.dialog_buy_item, null);
//                dialog = new AlertDialog.Builder(context).create();
//
//                TextView tv_clance = (TextView) view.findViewById(R.id.tv_clance);
//                tv_clance.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        dialog.dismiss();
//                    }
//                });
//
//                DisplayMetrics dm = new DisplayMetrics();
//                ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(dm);
//                int x = dm.widthPixels;// 屏幕的宽
//                int y = dm.heightPixels;// 屏幕的宽
//                dialog = new Dialog(context, R.style.dialog);
//                dialog.getWindow().setLayout((int) (x / 2.2), (int) (y / 1.8));
//                dialog.show();
//                dialog.setContentView(view);
//                dialog.setCancelable(false);
                break;
            default:
                break;
        }
        //刷新ListView内容
        notifyDataSetChanged();
    }
}
